#pragma once

#include <array>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

namespace uppg32
{
	class statistics
	{
	private:
		array<int, 1000000> stats;
		int numEntries;

		void readfile(string filename)
		{
			numEntries = 0;
			stats.fill(0);
			ifstream in(filename);
			string line;
			while (getline(in, line))
			{
				istringstream iss(line);
				int n;
				iss >> n;
				if (n >= 20000 && n <= 1000000)
				{
					stats[n]++;
					numEntries++;
				}				
			}
		}

	public:

		statistics()
		{
			readfile("salary.txt");
		}

		statistics(string filename)
		{
			readfile("salary.txt");
		}

		~statistics()
		{
		}

		void clear()
		{
			stats.fill(0);
		}

		int calcMean()
		{
			long long sum = 0;
			for (int i = 20000; i < 1000000; i++)
			{
				if (stats[i] > 0)
				{
					sum += i * stats[i];
				}				
			}
			return sum / numEntries;
		}

		int calcMedian()
		{
			int count = 0;
			for (int i = 20000; i < 1000000; i++)
			{
				count += stats[i];
				if (count == numEntries / 2)
				{
					return i;
				}
			}
		}
	};
}

